package com.a16.yuejian.yuejian.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.a16.yuejian.yuejian.R;
import com.a16.yuejian.yuejian.activity.TrDetailActivity;
import com.a16.yuejian.yuejian.javabean.Train;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by 55080 on 2017/10/19.
 */

public class TrainAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context mContext;
    private List mDataList;
    private LayoutInflater mLayoutInflater;
    public TrainAdapter(Context mContext){
        this.mContext = mContext;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    public void setList(List mDataList){
        this.mDataList = mDataList;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mLayoutInflater.from(parent.getContext()).inflate(R.layout.train_cardview,parent,false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Train entity = (Train) mDataList.get(position);
        if (null == entity)
            return;
        final ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.train_name.setText(entity.getTrainname());
        Picasso.with(mContext).load("http://10.0.2.2:8080/neu_yuejian201/public/photo/"+entity.getPic()).into(viewHolder.train_img);
        viewHolder.train_container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, TrDetailActivity.class);
                intent.putExtra("trainid",entity.getTrainid());
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        ImageView train_img;
        TextView train_name;
        FrameLayout train_container;

        public ViewHolder(View itemView) {
            super(itemView);
            train_name = (TextView) itemView.findViewById(R.id.train_name);
            train_img = (ImageView) itemView.findViewById(R.id.train_img);
            train_container= (FrameLayout) itemView.findViewById(R.id.train_container);
        }
    }
}
